//
//  StringMessageDecoder.m
//  PinBan
//
//  Created by kenta.yao on 15/11/4.
//  Copyright © 2015年 centling. All rights reserved.
//

#import "StringMessageDecoder.h"
#import "NSObject+Ext.h"


@implementation StringMessageDecoder

+ (NettyMessage *)nettyMessageWithData:(NSData *)data{

    NettyMessage *nettyMessage = [[NettyMessage alloc] init];
    
    MessageHeader *header= [[MessageHeader alloc]init];
    
    NSMutableData *mutableData = [NSMutableData dataWithData:data];

    NSData *sessionIDData = [mutableData subdataWithRange:NSMakeRange(0, 8)];
    NSLog(@"%@",sessionIDData);
    Byte *sessionIdByte = (Byte *)[sessionIDData bytes];
    Byte sessionBytes[8];
    for(int i=0;i<8;i++)
    {
        sessionBytes[i] = sessionIdByte[i];
    }
    
    header.sessionID=  ((((long) sessionBytes[ 0] & 0xff) << 56)
                        | (((long) sessionBytes[ 1] & 0xff) << 48)
                        | (((long) sessionBytes[ 2] & 0xff) << 40)
                        | (((long) sessionBytes[ 3] & 0xff) << 32)
                        | (((long) sessionBytes[ 4] & 0xff) << 24)
                        | (((long) sessionBytes[ 5] & 0xff) << 16)
                        | (((long) sessionBytes[ 6] & 0xff) << 8)
                        | (((long) sessionBytes[ 7] & 0xff) < 0));
    

    NSString *sessionIDString = [[NSString alloc] initWithData:sessionIDData encoding:NSUTF8StringEncoding];
    long sessionID = [sessionIDString integerValue];
    NSData *typeData = [mutableData subdataWithRange:NSMakeRange(8, 1)];
    Byte type = (Byte)[typeData bytes];
    Byte *typeByte = (Byte *)[typeData bytes];
    Byte typeBytes[1];
    for(int i=0;i<1;i++)
    {
        typeBytes[i]=typeByte[i];
    }
    header.type=typeBytes[0] & 0xff;
    NSData *priorityData = [mutableData subdataWithRange:NSMakeRange(9, 1)];
    Byte priority = (Byte)[priorityData bytes];
    NSData *attachmentData = [mutableData subdataWithRange:NSMakeRange(10, 4)];
    NSString *attachmentString = [[NSString alloc] initWithData:attachmentData encoding:NSUTF8StringEncoding];
    int attachment = [attachmentString intValue];
    
    header.sessionID = sessionID;
    header.type = type;
    header.priority = priority;

    
    // 判断type类型，解析body
    
    int typeInt = [self dataToInt:typeData];
    
    if (typeInt == 4) {
        
    }
    
    MessageBody *context =[[MessageBody alloc] init];
    
    //测试数据
    // body length (14 4) 对应的value 得值
    // body
    NSData *bodySizeData= [mutableData subdataWithRange:NSMakeRange(14, 4)];
    int length = [self dataToInt:bodySizeData];
//    Byte *bodyByte = (Byte *)[bodyData bytes];
//    //    int len=([testData length] -22);
//    int length = [[NSString stringWithFormat:@"%lu",(unsigned long)(bodyData.length)] intValue]/2;

    //解析body
    
    NSData *bodyData = [mutableData subdataWithRange:NSMakeRange(18, length)];
    NSLog(@"%@",bodyData);
    NSString *contextStr = [[NSString alloc]initWithData:bodyData encoding:NSUTF8StringEncoding];
    
    NSData *ddddd = [contextStr dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:ddddd options:kNilOptions error:&err];
    NSLog(@"%@",dic);
    nettyMessage.nettyMessageHeader = header;
    nettyMessage.nettyMessageBody = context;
    
    return nettyMessage;


}

+(int) dataToInt:(NSData *) data
{
    unsigned char bytes[4];
    [data getBytes:bytes length:4];
    uint32_t n = (int)bytes[0] << 24;
    n |= (int)bytes[1] << 16;
    n |= (int)bytes[2] << 8;
    n |= (int)bytes[3];
    
    return n;
}
@end
